package com.yhy.wxzn.wcs.device.netty.commons;

import com.yhy.wxzn.wcs.device.netty.bean.SiemensMessage;
import com.yhy.wxzn.wcs.device.netty.enumerate.DataType;
import com.yhy.wxzn.wcs.device.netty.enumerate.FnCode;
import io.netty.buffer.ByteBuf;
import io.netty.util.internal.StringUtil;

public class Utility {
    public Utility() {
    }

    public static int byteToInt(byte value) {
        return value & 255;
    }

    public static SiemensMessage getAddress(String addr) {
        SiemensMessage msg = null;
        String address = addr.toUpperCase();
        if (!StringUtil.isNullOrEmpty(address) && address.length() > 2) {
            address = address.replace("DBX", "");
            address = address.replace("DB", "E");
            address = address.replace("DW", "L");
            String[] arr = address.split("\\.");
            int arrLength = arr.length;
            FnCode fnCode = getFnCode(arr[0]);
            DataType dataType = null;
            int addressStart = 0;
            int dbNumber = 0;
            boolean isByte = true;
            if (arrLength == 1) {
                dataType = getDataType(arr[0]);
                addressStart = getAddressStart(arr[0]);
            } else if (arrLength == 2) {
                if (fnCode != FnCode.DB) {
                    isByte = false;
                    dataType = DataType.Bit;
                    addressStart = getBitStart(arr[0], arr[1]);
                } else {
                    dataType = getDataType(arr[1]);
                    addressStart = getAddressStart(arr[1]);
                    dbNumber = getDBNumber(arr[0]);
                }
            } else if (arrLength == 3 && fnCode == FnCode.DB) {
                isByte = false;
                dbNumber = getDBNumber(arr[0]);
                addressStart = getBitStart(arr[1], arr[2]);
                dataType = DataType.Bit;
            }

            if (fnCode != null && dataType != null && addressStart >= 0 && dbNumber >= 0) {
                msg = new SiemensMessage();
                msg.setFnCode(fnCode);
                msg.setDbBlockNo(dbNumber);
                msg.setDataType(dataType);
                msg.setDataAddress(addressStart);
                msg.setByte(isByte);
            }
        }

        return msg;
    }

    private static FnCode getFnCode(String address) {
        String fcCode = address.substring(0, 1);
        FnCode temp = null;
        byte var4 = -1;
        switch(fcCode.hashCode()) {
            case 69:
                if (fcCode.equals("E")) {
                    var4 = 3;
                }
                break;
            case 73:
                if (fcCode.equals("I")) {
                    var4 = 0;
                }
                break;
            case 77:
                if (fcCode.equals("M")) {
                    var4 = 2;
                }
                break;
            case 81:
                if (fcCode.equals("Q")) {
                    var4 = 1;
                }
        }

        switch(var4) {
            case 0:
                temp = FnCode.I;
                break;
            case 1:
                temp = FnCode.Q;
                break;
            case 2:
                temp = FnCode.M;
                break;
            case 3:
                temp = FnCode.DB;
        }

        return temp;
    }

    private static DataType getDataType(String address) {
        if (!Character.isDigit(address.charAt(1))) {
            address.substring(1, 2);
        } else {
            String var10000 = "b";
        }

        DataType length = null;
        String dataType = address.substring(1, 2);
        byte var4 = -1;
        switch(dataType.hashCode()) {
            case 66:
                if (dataType.equals("B")) {
                    var4 = 2;
                }
                break;
            case 68:
                if (dataType.equals("D")) {
                    var4 = 4;
                }
                break;
            case 76:
                if (dataType.equals("L")) {
                    var4 = 5;
                }
                break;
            case 87:
                if (dataType.equals("W")) {
                    var4 = 3;
                }
                break;
            case 88:
                if (dataType.equals("X")) {
                    var4 = 1;
                }
                break;
            case 98:
                if (dataType.equals("b")) {
                    var4 = 0;
                }
        }

        switch(var4) {
            case 0:
            case 1:
                length = DataType.Bit;
                break;
            case 2:
                length = DataType.Byte;
                break;
            case 3:
                length = DataType.Word;
                break;
            case 4:
                length = DataType.Dint;
                break;
            case 5:
                length = DataType.Long;
        }

        return length;
    }

    private static int getDBNumber(String address) {
        int temp = -1;

        try {
            temp = Integer.parseInt(address.substring(1));
        } catch (Exception var3) {
        }

        return temp;
    }

    private static int getBitStart(String addsByte, String addsBit) {
        int temp1 = -1;
        int temp2 = -1;

        try {
            int index = getNumberIndex(addsByte);
            if (index >= 0) {
                temp1 = Integer.parseInt(addsByte.substring(index));
            }

            temp2 = Integer.parseInt(addsBit);
        } catch (Exception var5) {
        }

        return temp1 * 8 + temp2;
    }

    private static int getAddressStart(String address) {
        int index = getNumberIndex(address);
        int temp = -1;
        String str = null;
        if (index > 0) {
            try {
                str = address.substring(index);
                temp = Integer.parseInt(str);
            } catch (Exception var5) {
            }
        }

        return temp;
    }

    private static int getNumberIndex(String str) {
        for(int i = 0; i < str.length(); ++i) {
            if (Character.isDigit(str.charAt(i))) {
                return i;
            }
        }

        return -1;
    }

    public static String bytesToHexStr(ByteBuf in, int offset, int count) {
        String temp = "";
        for(int i = offset; i < count; ++i) {
            int n = in.getByte(i) & 255;
            temp = temp + String.format("%02X", n) + " ";
        }
        return temp;
    }
}
